<template>
  <div>
    <el-button type="primary" size="default" icon="ele-Plus" @click="toAddSpu"
    >添加SPU
    </el-button
    >
    <el-table v-loading="loading" border stripe style="margin: 20px 0" :data="spuInfoList">
      <el-table-column type="index" width="80" label="序号"/>
      <el-table-column label="Spu名称" prop="spuName"></el-table-column>
      <el-table-column label="Spu描述" prop="description"></el-table-column>
      <el-table-column label="操作">
        <template v-slot="{ row }">
          <el-button
              title="添加sku"
              type="primary"
              size="small"
              icon="ele-Plus"
              @click="toAddSku(row)"
          ></el-button>
          <el-button
              title="修改spu"
              type="primary"
              size="small"
              icon="ele-Edit"
              @click="toUpdateSpu(row)"
          ></el-button>
          <el-button @click="lookSku(row.id)" title="查看sku" type="info" size="small" icon="ele-Warning"></el-button>
          <el-button @click="removeSpu(row.id)" title="删除spu" type="danger" size="small" icon="ele-Delete"></el-button>
        </template>
      </el-table-column>
    </el-table>
    <!--    分页器-->
    <el-pagination
        v-model:page-size="pageSize"
        :page-count="pageCount"
        :pager-count="5"
        :page-sizes="[3, 5, 7, 9]"
        v-model:current-page="currentPage"
        layout=" prev, pager, next, sizes"
    />

    <!--   点击查看 sku弹窗-->
    <el-dialog v-model="dialogVisible" title="SKU列表" width="70%">
      <el-table border stripe :data="skuInfoList">
        <el-table-column label="名称" prop="skuName"></el-table-column>
        <el-table-column label="价格" prop="price"></el-table-column>
        <el-table-column label="重量" prop="weight"></el-table-column>
        <el-table-column label="默认图片">
          <template v-slot="{ row }">
            <img :src="row.skuDefaultImg" alt="" height="100"/>
          </template>
        </el-table-column>
      </el-table>

      <template #footer>
        <span class="dialog-footer">
          <el-button type="primary" @click="dialogVisible = false">
            关闭
          </el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script lang="ts">
import {defineComponent} from "vue";

export default defineComponent({
  name: "SpuList",
});
</script>

<script setup lang="ts">
import {ref, watch} from "vue";
import {ElMessage} from "element-plus";
import useCategoryStore from "@/stores/category";
import {storeToRefs} from "pinia";
import {reqDeleteSpuAsync, reqGetSpuListAsync, type SpuItemType, type SpuListType} from "@/api/spu";
import type {SkuItemType, SkuListType} from "@/api/sku";
import {reqGetSkuListBySpuIdAsync} from "@/api/sku";

//定义页码需要的数据
//1.每页数量
const pageSize = ref(3);
//2.总页数
const pageCount = ref(0);
//3.当前页
const currentPage = ref(1);

//接收切换type的自定义事件
const emit = defineEmits<{
  (event: "changeType", type: number, row?: SpuItemType): void;
}>();
//切换组件的点击回调函数
const toAddSpu = () => {
  emit("changeType", 1);
};
const toAddSku = (row: SpuItemType) => {
  emit("changeType", 2, row);
};
const toUpdateSpu = (row: SpuItemType) => {
  emit("changeType", 1, row);
};
// 获取cateStore对象
const cateStore = useCategoryStore()
// 解构获得 3id
const {categoryList3Id} = storeToRefs(cateStore);
// 保存spu列表数据
const spuInfoList = ref<SpuListType>([])
// 控制发请求时表格的加载中效果
const loading = ref(false)
// 获得spu列表数据
const getSpuInfoList = async () => {
  try {
    const re = await reqGetSpuListAsync(currentPage.value, pageSize.value, categoryList3Id.value as number);
    // 重置数据
    spuInfoList.value = re.records
    pageCount.value = re.pages
    // 停止加载
    loading.value = false
  } catch (e: any) {
    ElMessage.error(e.message)
  }
}
// 当categoryList3Id 有值的时候就代表三级分类的数据查询完了
watch(categoryList3Id, (newVal) => {
  if (!newVal) return  // 为null的剔除
  // 加载中
  loading.value = true
  // 更新数据
  getSpuInfoList()
}, {immediate: true})  // immediate: true 立即监听 组件切换时 会导致组件重建和销毁 spuList会没有数据  categoryList3Id只要有值就应该立马更新数据
// 控制分页器的改变
watch([pageSize, currentPage], () => {
  // 加载中
  loading.value = true
  // 更新数据
  getSpuInfoList()
})

// 控制查看sku列表的显示隐藏
const dialogVisible = ref(false)

// 保存 sku的列表数据
const skuInfoList = ref<SkuListType>([])
// 获取sku的列表数据
const lookSku = async (spuId: number) => {
  // 弹窗显示
  dialogVisible.value = true
  try {
    skuInfoList.value = await reqGetSkuListBySpuIdAsync(spuId)
  } catch (e: any) {
    ElMessage.error(e.message)
  }
}

// 删除spu
const removeSpu = async (spuId: number) => {
  //请求数据
  try {
    await reqDeleteSpuAsync(spuId);
    ElMessage.success("删除成功");

    //发送初始化请求
    await getSpuInfoList()
  } catch (e: any) {
    ElMessage.error(e.message);
  }
}
</script>

<style scoped></style>
